package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;

/**
 * A class used for managing address
 */
public class AddressDAO {
	
	/** a database of this address */
    private Database db;
    
    /**
     * Instantiate a new object of this class
     * @param database a database
     */
    public AddressDAO(Database db) {
        this.db = db;
    }
    
    /**
     * a method for searching all address in database
     * @return all address
     */
    public List<Map<String, Object>> findAll() {
        String vaSql = "SELECT * FROM address";
        List<Map<String, Object>> result = db.queryList(vaSql);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    
    public List<Map<String, Object>> findProvince() {
    	String vaSql = "SELECT province, name FROM address WHERE city = '00' and district = '00' ORDER BY name ASC";
    	List<Map<String, Object>> result = db.queryList(vaSql);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    
    public List<Map<String, Object>> findDistrict(String paProvince) {
    	String vaSql = "SELECT district, name FROM address WHERE province = ? and city = '00' "
    			+ "and district not in '00' ORDER BY district ASC";
    	List<Map<String, Object>> result = db.queryList(vaSql, paProvince);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    
    public List<Map<String, Object>> findCity(String paProvince, String paDistrict) {
    	String vaSql = "SELECT city, name FROM address WHERE province = ? and district = ? "
    			+ "and city not in '00' ORDER BY city ASC";
    	List<Map<String, Object>> result = db.queryList(vaSql, paProvince, paDistrict);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    
    /**
     * find province_name by input province_id
     * @param paProvince
     * @return
     */
    public String getProvinceName(String paProvince) {
    	String vaSql = "SELECT name FROM address WHERE city = '00' and district = '00' and province = ?";
    	Map<String, Object> result = db.querySingle(vaSql, paProvince);
        if (result != null) {
            return result.get("NAME").toString();
        } else {
            return null;
        }
    }
    
    /**
     * find district_name by input province_id, district_id
     * @param paProvince
     * @param paDistrict
     * @return
     */
    public String getDistrictName(String paProvince, String paDistrict) {
    	String vaSql = "SELECT name FROM address WHERE province = ? and city = '00' and district = ?";
    	Map<String, Object> result = db.querySingle(vaSql, paProvince, paDistrict);
        if (result != null) {
            return result.get("NAME").toString();
        } else {
            return null;
        }
    }
    
    /** 
     * find city_name by input province_id, district_id, city_id
     * @param paProvince
     * @param paDistrict
     * @param paCity
     * @return
     */
    public String getCityName(String paProvince, String paDistrict, String paCity) {
    	String vaSql = "SELECT name FROM address WHERE province = ? and district = ? and city = ?";
    	Map<String, Object> result = db.querySingle(vaSql, paProvince, paDistrict, paCity);
        if (result != null) {
            return result.get("NAME").toString();
        } else {
            return null;
		}
	}

}
